import {
  getOffspringn,
  getOffspringnTree,
  getAncestry
} from '@/assets/js/util.js'

import _ from 'lodash'

export default {
  sidebar: state => state.app.sidebar,
  size: state => state.app.size,
  device: state => state.app.device,
  visitedViews: state => state.tagsView.visitedViews,
  cachedViews: state => state.tagsView.cachedViews,
  token: state => state.user.token,
  avatar: state => state.user.avatar,
  name: state => state.user.name,
  introduction: state => state.user.introduction,
  roles: state => state.user.roles,
  permission_routes: state => state.permission.routes,
  errorLogs: state => state.errorLog.logs,
  // 获取部门后代，平铺方式
  departmentListOffspringn(state) {
    const aOffspringn = getOffspringn(_.cloneDeep(state.departmentList), 0)
    for (var i = aOffspringn.length; i--;) {
      aOffspringn[i].children = getAncestry(
        _.cloneDeep(state.departmentList),
        aOffspringn[i].parentId
      )
    }
    return aOffspringn
  },
  // 取部门后代，树 children方式
  departmentListOffspringnTreeData(state) {
    const offspringnTreeData = getOffspringnTree(
      _.cloneDeep(state.departmentList),
      0
    )
    return offspringnTreeData
  },
  // 获取所有平台后代，树 children方式
  allPlatformListOffspringnTreeData: state => addAllType => {
    addAllType = addAllType || 0

    const arr = _.cloneDeep(state.allPlatformList)
    const topParentId = state.orginNestingPlatformList[0].parentId

    // 不需要添加全部
    if (!addAllType) {
      const offspringnTreeData = getOffspringnTree(arr, topParentId)
      return offspringnTreeData
    }

    // 全部基本不可选，用于详情 创建等
    if (addAllType === 1) {
      arr.push({
        id: topParentId,
        name: '全部',
        parentId: -1
      })

      const offspringnTreeData = getOffspringnTree(arr, -1)
      return offspringnTreeData
    }
    // 全部的id为空值，只是用于搜索
    if (addAllType === 2) {
      const offspringnTreeData = getOffspringnTree(arr, topParentId)

      offspringnTreeData.unshift({
        id: '',
        name: '全部',
        parentId: -1
      })

      return offspringnTreeData
    }
  },
  // 获取意向平台后代，树 children方式
  wantPlatformListOffspringnTreeData: state => addAllType => {
    addAllType = addAllType || 0

    const arr = _.cloneDeep(state.wantPlatformList)
    const topParentId = state.orginNestingPlatformList[0].parentId

    // 不需要添加全部
    if (!addAllType) {
      const offspringnTreeData = getOffspringnTree(arr, topParentId)
      return offspringnTreeData
    }

    // 全部基本不可选，用于详情 创建等
    if (addAllType === 1) {
      arr.push({
        id: topParentId,
        name: '全部',
        parentId: -1
      })

      const offspringnTreeData = getOffspringnTree(arr, -1)
      return offspringnTreeData
    }
    // 全部的id为空值，只是用于搜索
    if (addAllType === 2) {
      const offspringnTreeData = getOffspringnTree(arr, topParentId)

      offspringnTreeData.unshift({
        id: '',
        name: '全部',
        parentId: -1
      })

      return offspringnTreeData
    }
  },

  // 获取发布平台后代，树 children方式
  platformListOffspringnTreeData: state => addAllType => {
    addAllType = addAllType || 0

    const arr = _.cloneDeep(state.platformList)
    const topParentId = state.orginNestingPlatformList[0].parentId

    // 不需要添加全部
    if (!addAllType) {
      const offspringnTreeData = getOffspringnTree(arr, topParentId)
      return offspringnTreeData
    }

    // 全部基本不可选，用于详情 创建等
    if (addAllType === 1) {
      arr.push({
        id: topParentId,
        name: '全部',
        parentId: -1
      })

      const offspringnTreeData = getOffspringnTree(arr, -1)
      return offspringnTreeData
    }
    // 全部的id为空值，只是用于搜索
    if (addAllType === 2) {
      const offspringnTreeData = getOffspringnTree(arr, topParentId)

      offspringnTreeData.unshift({
        id: '',
        name: '全部',
        parentId: -1
      })

      return offspringnTreeData
    }
  },
  // 所有操作权限
  // allFns(state) {
  //   if (!state.userData) {
  //     return;
  //   }
  //   let functions = state.userData.functions || [];
  //   let fns = [];
  //   functions.forEach(item => {
  //     fns.push(item.frontRoute);
  //   });
  //   return fns;
  // }

  hasChildrenCorporations(state) {
    let hasChildrenCorporations
    if (!state.userData.corporations) {
      return hasChildrenCorporations
    } else if (state.userData.corporations.length > 1) {
      hasChildrenCorporations = true
    } else if (
      state.userData.corporations[0].children &&
      state.userData.corporations[0].children.length
    ) {
      hasChildrenCorporations = true
    }

    return hasChildrenCorporations
  }
}

